package com.mjf.spark.day04

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * 转换算子-reduceByKey
 *    根据相同的key对RDD中元素进行聚合
 */
object Spark02_Transformation_reduceByKey {
  def main(args: Array[String]): Unit = {

    // 创建SparkConf配置文件
    val conf = new SparkConf().setMaster("local[*]").setAppName("Spark02_Transformation_reduceByKey")
    // 创建SparkContext对象
    val sc = new SparkContext(conf)

    val rdd: RDD[(String, Int)] = sc.makeRDD(List(("a", 1), ("b", 3), ("a", 5), ("b", 2)))

    val resRDD: RDD[(String, Int)] = rdd.reduceByKey(_ + _)

    resRDD.collect().foreach(println)

    // 关闭连接
    sc. stop()

  }
}

